<template>
  <!-- 重置密码dialog -->
  <el-dialog title="预警配置" :visible.sync="visible" width="40%">
    <el-form
      :model="changeForm"
      label-width="90px"
      :rules="rules"
      ref="changeForm"
    >
      <el-form-item label="仓库名称" prop="stock">
        {{ initObj.name }}
      </el-form-item>
      <el-form-item
        v-for="(domain, index) in dynamicValidateForm.domains"
        :label="'域名' + index"
        :key="domain.key"
        :prop="'domains.' + index + '.value'"
        :rules="{
          required: true,
          message: '域名不能为空',
          trigger: 'blur',
        }"
      >
        <el-input v-model="domain.value"></el-input>
        <el-button @click.prevent="removeDomain(domain)">删除</el-button>
        <el-button @click="addDomain">删除</el-button>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="handelCancel">取 消</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
  <script>
import { setWarningConf, waringConfInfo } from "@/api/service/inventory";

export default {
  data() {
    return {
      initObj: {},
      visible: false,
      changeForm: {
        stock: "",
      },
      rules: {
        stock: [
          {
            required: true,
            message: "请输入",
            trigger: "change",
          },
        ],
      },
    };
  },
  props: {},
  methods: {
    // 初始化函数--->父级调用
    handelInit(val) {
      console.log(val);
      if (val.id) {
        this.getInventoryFn(val);
      } else {
        this.$message.error("获取仓库详情失败");
      }
      this.initObj = val;
      this.visible = true;
    },
    getInventoryFn(val) {
      waringConfInfo(val.id).then((res) => {
        console.log(res);
        // let { code, msg } = res;
        // if (code == "200") {
        //   this.$message.success("库存修改成功");
        //   this.$refs.changeForm.resetFields();
        //   this.visible = false;
        //   this.$emit("updataList");
        // } else {
        //   this.$message.error(msg);
        // }
      });
    },
    handelCancel() {
      this.$refs.changeForm.resetFields();
      this.visible = false;
    },
    handleSubmit() {
      this.$refs.changeForm.validate((valid) => {
        if (valid) {
          this.handleValidateSuccess();
        } else {
          return false;
        }
      });
    },
    handleValidateSuccess() {
      let obj = {
        id: this.initObj.id,
        stock: this.changeForm.stock,
      };
      setWarningConf(obj).then((res) => {
        console.log(res);
        let { code, msg } = res;
        if (code == "200") {
          this.$message.success("库存修改成功");
          this.$refs.changeForm.resetFields();
          this.visible = false;
          this.$emit("updataList");
        } else {
          this.$message.error(msg);
        }
      });
    },
  },
};
</script>